package com.gnetek.netty.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.gnetek.netty.contact.Contact;
import com.gnetek.netty.filter.HexTextFilter;
import com.gnetek.netty.filter.NumberTextFilter;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @Description 设置界面控制类
 * @Author Darren Huang
 * @Date 2023-09-08 12:13
 */
public class SettingController implements Initializable {
    private Logger log = LoggerFactory.getLogger(SettingController.class);

    @FXML
    private TextField beehiveMacTextField;
    @FXML
    private TextField swarmMacTextField;
    @FXML
    private TextArea honeybeeMacTextArea;

    private String settingFilePath = null;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
//        settingFilePath = this.getClass().getClassLoader().getResource("").getPath();
        settingFilePath = System.getProperty("user.dir");
        if(settingFilePath.startsWith("/")){
            settingFilePath = settingFilePath.substring(1);
        }
        settingFilePath += "/json/setting.json";
        log.error(settingFilePath);

        String content = FileUtil.readString(settingFilePath, "utf-8");
        log.error(content);

        if(StrUtil.isNotBlank(content)){
            JSONObject setting = JSONUtil.parseObj(content);
            beehiveMacTextField.setText(setting.getStr(Contact.BEEHIVE_ID));
            swarmMacTextField.setText(setting.getStr(Contact.SWARM_IDS));
            honeybeeMacTextArea.setText(setting.getStr(Contact.HONEYBEE_IDS));
        }

        final HexTextFilter hexTextFilter = new HexTextFilter(8);
        beehiveMacTextField.setTextFormatter(new TextFormatter<String>(hexTextFilter));
    }

    @FXML
    public void saveSetting(ActionEvent e){
        JSONObject setting = new JSONObject();

        String beehiveId = beehiveMacTextField.getText();
        if (StrUtil.isNotBlank(beehiveId)) {
            setting.set(Contact.BEEHIVE_ID, beehiveId);

            String swarmIdStr = swarmMacTextField.getText();
            if(StrUtil.isNotBlank(swarmIdStr)){
                setting.set(Contact.SWARM_IDS, swarmIdStr);

                String honeybeeIdStr = honeybeeMacTextArea.getText();
                if(StrUtil.isNotBlank(honeybeeIdStr)){
                    setting.set(Contact.HONEYBEE_IDS, honeybeeIdStr);
                }
            }
        }

        log.error("setting json = {}", setting);

        FileUtil.writeString(setting.toString(), settingFilePath, "utf-8");

        ((Stage)((Button)e.getSource()).getScene().getWindow()).close();
    }
}
